package com.seth.myapplication.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.seth.myapplication.R;
import com.seth.myapplication.adapter.ItemAdapter;
import com.seth.myapplication.bean.Rewords;
import com.seth.myapplication.bean.User;
import com.seth.myapplication.service.NetWorkService;
import com.seth.myapplication.surface.page3Fragment;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Seth on 2017/11/20.
 */

abstract public class TaskSetFragment extends Fragment {

    protected RecyclerView mRecyclerView;
    protected MaterialRefreshLayout refreshTask;
    protected ArrayList<Rewords> rewords = new ArrayList<>();
    protected ItemAdapter taskItemAdapter;
    protected TaskBroadCast msgReceiver;


    public abstract View initStartView(LayoutInflater inflater,ViewGroup container);
    public abstract void getNetTask();
    public abstract String newBroadCast();
    protected abstract ArrayList<Rewords> DealUsers(ArrayList<Rewords> rewords, HashMap<String, User> users, Intent intent);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //view = inflater.inflate(R.layout.fragment_get_task, container, false);
        View view = initStartView(inflater, container);
        initView(view);

        initEvent();


        return view;

    }

    private void initEvent() {
        getNetTask();
        refreshTask.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {
                //refreshing...
                getNetTask();
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                //load more refreshing...
            }
        });
    }

    private void initView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.list_task);
        refreshTask = (MaterialRefreshLayout) view.findViewById(R.id.refresh_task);
        taskItemAdapter = new ItemAdapter(getContext());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(taskItemAdapter);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Override
    public void onAttach(Context context) {

        //动态注册广播接收器
        msgReceiver = new TaskBroadCast();
        IntentFilter intentFilter = new IntentFilter();
        String str = newBroadCast();
        intentFilter.addAction(str);
        context.registerReceiver(msgReceiver, intentFilter);
        super.onAttach(context);
    }

    class TaskBroadCast extends BroadcastReceiver
    {
        HashMap<String,User> users;
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String msg = intent.getStringExtra("result");

            if("success".equals(msg)){
                rewords = (ArrayList<Rewords>) intent.getSerializableExtra("rewords");
                users = DealRewords(rewords);
            }else if("userinfo".equals(msg)){
                //添加同步互斥
                rewords = DealUsers(rewords,users,intent);

            }else{
                refreshTask.finishRefresh();
                taskItemAdapter.setList(rewords);
                taskItemAdapter.notifyDataSetChanged();
                Toast.makeText(context, "获取悬赏失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    protected abstract HashMap<String,User> DealRewords(ArrayList<Rewords> rewords);

}
